<?php
namespace app\super\controller;
use think\Db;

class ProjectController extends CommonController
{
    /**
     *团队成员
     */
    public function getCourseJSON()
    {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        if($_POST['user_phone']!=''){
            $user_phone = $_POST['user_phone'];
            $where['u.user_phone|u.user_username|ut.team_name'] = array('like',"%$user_phone%");
        }
        if($_POST['role']!=''){
            $where['team_member_role'] = $_POST['role'];
        }
        if($_POST['join_time']!=''){
            $where['team_member_join_time'] = array('EGT',$_POST['join_time']);
        }
        $where['team_member_remove'] = 2;
        $res = Db::table('up_team_members')->alias('a')
                                    ->where($where)
                                    ->join('up_u_user u','u.user_id=a.team_member_user_id','left')
                                    ->join('up_u_user uuu','uuu.user_id=a.team_member_up_user','left')
                                    ->join('up_team ut','ut.team_id = a.team_id','left')
                                    ->field('a.*,u.user_username,u.user_phone,uuu.user_username as up_user,ut.team_name')
                                    ->limit($rows * ($page - 1), $rows)
                                    ->select();
        $total = Db::table('up_team_members')->alias('a')
            ->where($where)
            ->join('up_u_user u','u.user_id=a.team_member_user_id','left')
            ->join('up_u_user uuu','uuu.user_id=a.team_member_up_user','left')
            ->join('up_team ut','ut.team_id = a.team_id','left')
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

   /**
    * 导出下载
    */
//   public function excelDownload(){
//       if($_POST['user_phone']!=''){
//           $user_phone = $_POST['user_phone'];
//           $where['u.user_phone|u.user_username'] = array('like',"%$user_phone%");
//       }
//       if($_POST['role']!=''){
//           $where['team_member_role'] = $_POST['role'];
//       }
//       if($_POST['join_time']!=''){
//           $where['team_member_join_time'] = array('EGT',$_POST['join_time']);
//       }
//       $where['team_id'] = 1;//TODO 缺少团队ID
//
//       $where['team_member_remove'] = 2;
//       $res = Db::table('up_team_members')->alias('a')
//           ->where($where)
//           ->join('up_u_user u','u.user_id=a.team_member_user_id','left')
//           ->join('up_u_user uuu','uuu.user_id=a.team_member_up_user','left')
//           ->field('u.user_username,u.user_phone,uuu.user_username as up_user,a.team_member_role,a.team_member_join_time')
//           ->order('a.team_member_join_time desc')
//           ->select();
//       $xlsCell  = array(
//           array('user_username','昵称'),
//           array('user_phone','手机号码'),
//           array('up_user','所属客户'),
//           array('team_member_role','角色'),
//           array('team_member_join_time','加入时间')
//       );
//       //TODO 缺少团队名称
//       $data = $this->exportExcel('aaa',$xlsCell,$res);
//       return $data;
//   }

    /**
     * 添加成员页面
     */
//   public function addMember(){
//
//       $where['team_id'] = 1;//TODO 缺少团队ID
//       $where['team_member_remove'] = 2;
//       $user_id = Db::table('up_team_members')->where($where)->column('team_member_user_id');
//       $whereMap['user_id'] = array('not in',$user_id);
//       $user_data = Db::table('up_u_user')->where($whereMap)->field('user_id,user_username,user_phone')->select();
//       $this->assign('user_data',$user_data);
//       return $this->fetch("member/addMember");
//   }

    /**
     * @return string添加团队成员
     */
//   public function addTeamMember(){
//       $user = input('user/a');
//       if(count($user)<=0){
//           $returnData['status'] = 0;
//           $returnData['msg'] ="选择的人员不能为空";
//           return json_encode($returnData);
//       }else{
//           $user_string =implode(',',$user);
//           $groupId = 1;
//           $groupName ='超级团队';
//           $data['str'] = $user_string;
//           $data['groupId'] = $groupId;
//           $data['groupName'] = $groupName;
//           $url = '192.168.2.123:8080/team/addPhpTeam.do';//TODO 缺少url连接地址
//           $msg = json_decode($this->httpPost($url,$data),true);
//           if ($msg['msg']==200) {
//               $array = array();
//               foreach ($user as $key=>$vo){
//                    $data['team_member_id'] = $this->getRandChar(32);
//                    $data['team_id'] = 1; //TODO 缺少团队ID
//                    $data['team_member_user_id']= $vo;
//                    $data['team_member_role']= 3;
//                    $data['team_member_join_time']= date('Y-m-d H:i:s');
//                   $array[] =$data;
//               }
//               $res = Db::table('up_team_members')->insertAll($array);
//               if($res){
//                   return json_encode(array("status" => 1, "msg" => "团队成员添加成功！"));
//
//               }else{
//                   return json_encode(array("status" => 0, "msg" => "团队成员添加失败！"));
//
//
//               }
//           } else {
//               return json_encode(array("status" => 0, "msg" => "团队成员添加失败！"));
//           }
//       }
//   }

   /**
    * 成员导入页面
    */
//   public function ImportUser(){
//
//    return $this->fetch('member/ImportUser');
//   }

   /**
    * 导入功能
    */
//   public function insertImportUser(){
//        $file_name = $_FILES['file']['name'];//上传的文件名字
//        $file_path = $_FILES['file']['tmp_name'];//上传的临时路径
//        $file_types = explode('.',$file_name);//切割上传文件名字
//        $file_type = $file_types [count ( $file_types ) - 1];//上传的名字后缀
//
//       set_time_limit(0);
////       vendor("phpoffice.phpexcel.Classes.PHPExcel");
//       vendor("phpoffice.phpexcel.Classes.PHPExcel.IOFactory");
//       if( $file_type == 'xls' ){
//           $inputFileType = 'Excel5';    //这个是读 xls的
//       }else{
//           $inputFileType = 'Excel2007';//这个是计xlsx的
//       }
//       $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
////       $objPHPExcel = $objReader ->load($file_path,$encode='utf-8');
//       $objPHPExcel = $objReader->load($file_path);
//
//       $objWorksheet = $objPHPExcel->getActiveSheet();
//
//       $highestRow = $objWorksheet->getHighestRow();
//       $highestColumn = $objWorksheet->getHighestColumn();
//        print_r($highestRow);
//        print_r($highestColumn);
//   }
}